local bendi = fk.CreateSkill {
  name = "lingling__bendi",
}

Fk:loadTranslationTable {
  ["lingling__bendi"] = "奔镝",
  [":lingling__bendi"] = "当你使用【杀】或【决斗】指定目标后，你可以失去1点体力并获得其一张牌，然后此次对其造成的伤害+1。" ..
  "若未对其造成伤害，当其下回合首次使用【杀】或【决斗】对你造成伤害后，你获得1点护甲。",

  ["#lingling__bendi-invoke"] = "奔镝：你可以失去1点体力，获得 %dest 一张牌，然后此次对其造成的伤害+1。",
  ["@@lingling__bendi"] = "奔镝",
}

bendi:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    local to = data.to
    return target == player and player:hasSkill(bendi.name) and data.card and
        to and not to.dead and (data.card.trueName == "slash" or data.card.trueName == "duel")
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = bendi.name,
      prompt = "#lingling__bendi-invoke::" .. data.to.id
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    if to.dead or player.dead then return end
    --room:setPlayerMark(to, "@@lingling__bendi", 1)
    room:loseHp(player, 1, bendi.name)
    if player.dead or to.dead then return end
    if not to:isNude() then
      local card = room:askToChooseCard(player, {
        target = to,
        flag = "he",
        skill_name = bendi.name,
      })
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, bendi.name, nil, false, player)
    end
    data.extra_data = data.extra_data or {}
    data.extra_data.lingling__bendi = data.extra_data.lingling__bendi or {}
    table.insertIfNeed(data.extra_data.lingling__bendi, to.id)
  end,
})

bendi:addEffect(fk.DamageInflicted, {
  can_refresh = function(self, event, target, player, data)
    if player.dead or data.card == nil or target ~= player then return false end
    local room = player.room
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if not use_event then return false end
    local use = use_event.data
    return use.card == data.card and use.extra_data and table.contains(use.extra_data.lingling__bendi, data.to.id)
  end,
  on_refresh = function(self, event, target, player, data)
    data:changeDamage(1)
  end,
})

bendi:addEffect(fk.CardUseFinished,{
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    if not player.dead and data.extra_data and data.extra_data.lingling__bendi then
      for index, tpID in ipairs(data.extra_data.lingling__bendi) do
        local to = player.room:getPlayerById(tpID)
        if to:isAlive() and not (data.damageDealt and data.damageDealt[to]) then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for index, tpID in ipairs(data.extra_data.lingling__bendi) do
        local to = player.room:getPlayerById(tpID)
        if to:isAlive() and not (data.damageDealt and data.damageDealt[to]) then
          room:setPlayerMark(to, "@@lingling__bendi", 1)
        end
    end
  end,
})

bendi:addEffect(fk.Damaged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(bendi.name) and data.from and
        data.from:getMark("@@lingling__bendi") > 0 and player.room.current == data.from and
        data.card and (data.card.trueName == "slash" or data.card.trueName == "duel")
  end,
  on_cost = Util.TrueFunc,
  on_trigger = function(self, event, target, player, data)
    player.room:changeShield(player, 1)
    player.room:setPlayerMark(data.from, "@@lingling__bendi", 0)
  end,
})

bendi:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(bendi.name) and target:getMark("@@lingling__bendi") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "@@lingling__bendi", 0)
  end
})

bendi:addLoseEffect(function(self, player, is_death)
  local room = player.room
  for _, p in ipairs(room:getOtherPlayers(player)) do
    room:setPlayerMark(p, "@@lingling__bendi", 0)
  end
end)

return bendi
